package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.dto.GdpDTO;
import com.css.fxfzypg.modules.baseData.queryParams.GdpQueryParams;
import com.css.fxfzypg.modules.baseData.entity.GdpEntity;
import com.css.fxfzypg.modules.baseData.service.GdpService;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-11 16:51
 * GDP数据子模块
 */
@RestController
@RequestMapping("/fxfzypg/baseData")
@Slf4j
public class GdpController {

    @Resource
    private GdpService gdpService;

    @PostMapping("/saveGdp")
    public RestResponse saveGdp(@RequestBody GdpEntity gdpEntity) {
        RestResponse restResponse = null;
        try {
            // int i = gdpService.saveGdp(gdpEntity);
            restResponse.setMessage("功能缺失！ ");
        } catch (Exception e) {
            log.error("新增gdp数据失败", e);
            RestResponse.fail("新增gdp数据失败");
        }
        return restResponse;
    }

    @GetMapping("/getGdpById/{id}")
    public RestResponse getGdpById(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(gdpService.getGdpById(id));
        } catch (Exception e) {
            log.error("获取GDP数据失败!", e);
            restResponse = RestResponse.fail("获取GDP数据失败!");
        }
        return restResponse;

    }

    @PostMapping("/updateGdp")
    public RestResponse updateGdp(@RequestBody GdpEntity gdpEntity) {
        RestResponse restResponse = null;
        try {
            int i = gdpService.updateGdp(gdpEntity);
            if (i > 0) {
                restResponse = ResponseCreator.createSuccessResponse(null,"更新Gdp成功！");
            } else {
                restResponse = RestResponse.fail("更新Gdp失败!");
            }
        } catch (Exception e) {
            log.error("更新Gdp失败", e);
            restResponse = RestResponse.fail("更新Gdp失败");
        }
        return restResponse;
    }

    @GetMapping("/getAllGdp")
    public RestResponse getAllGdp(HttpServletRequest request, GdpQueryParams queryParams) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = gdpService.getAllGdpByParams(queryParams, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            log.error("查询GDP数据失败!", e);
            restResponse = RestResponse.fail("查询GDP数据失败");
        }
        return restResponse;
    }

    @PostMapping("/deleteGdp/{id}")
    public RestResponse deletePeo(@PathVariable("id") String id) {
        try {
            gdpService.deleteGdp(id);
            return ResponseCreator.createSuccessResponse(null,"删除成功");
        } catch (Exception e) {
            log.error("删除失败", e);
            return RestResponse.fail("删除失败");
        }
    }


    @PostMapping("/gdpExportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        try {
            gdpService.exportExcel(response);
            ResponseCreator.createSuccessResponse(null,"导出成功");
        } catch (IOException e) {
            log.error("导出失败!", e);
            RestResponse.fail("导出失败!");
        }
    }


    @PostMapping("/gdpImportExcel")
    public RestResponse importExcel(MultipartFile file) throws IOException {
        RestResponse restResponse = null;
        try {
            restResponse = gdpService.importExcel(file);
        } catch (IOException e) {
            log.error("导入失败!", e);
            restResponse = RestResponse.fail("导入失败!");
        }
        return restResponse;
    }

    @GetMapping("/getLanderRoles")
    public RestResponse getLanderRoles()  {
        RestResponse restResponse=null;
        try {
            String roles=gdpService.getLanderRoles();
            restResponse= RestResponse.succeed(roles);
        } catch (Exception e) {
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("/getLanderProvince")
    public RestResponse getLanderProvince()  {
        RestResponse restResponse=null;
        try {
            String roles=gdpService.getLanderProvince();
            restResponse= RestResponse.succeed(roles);
        } catch (Exception e) {
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


}
